package com.czl.elm.controller;

import com.alibaba.fastjson.JSONObject;

import com.czl.elm.util.ActionResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping("file")
@Slf4j
public class FileController {
    @Value("${file.upload.url}")
    private String uploadFilePath;
    @Value("${file.download.url}")
    private String downloadFilePath;

    @ApiOperation(value = "上传文件")
    @PostMapping("/uploadone")
    public ActionResult httpUploadone(@RequestParam("file") MultipartFile file){
        if(file==null)return ActionResult.error("文件为空");
        System.out.println(uploadFilePath);
        System.out.println(file.getName());
        String fileName = file.getOriginalFilename();  // 文件名
        File dest = new File(uploadFilePath +'/'+ fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            log.error("{}",e);

            return ActionResult.error("上传文件失败");
        }


        return ActionResult.success("上传文件成功");
    }
    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public ActionResult httpUpload(@RequestParam("files") MultipartFile files[]){
        if(files==null)return ActionResult.error("文件为空");
        for(int i=0;i<files.length;i++){
            System.out.println(files[i].getName());
            String fileName = files[i].getOriginalFilename();  // 文件名
            File dest = new File(uploadFilePath +'/'+ fileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                files[i].transferTo(dest);
            } catch (Exception e) {
                log.error("{}",e);

                return ActionResult.error("上传文件失败");
            }
        }

        return ActionResult.success("上传文件成功");
    }

    @ApiOperation(value = "下载文件")
    @RequestMapping("/download")
    public String fileDownLoad(HttpServletResponse response,  String fileName){
        File file = new File(downloadFilePath +'/'+ fileName);
        if(!file.exists()){
            return "下载文件不存在";
        }
        response.reset();
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName );

        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));) {
            byte[] buff = new byte[1024];
            OutputStream os  = response.getOutputStream();
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            log.error("{}",e);
            return "下载失败";
        }
        return "下载成功";
    }


}
